package com.register.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.register.dao.QRDepotMapper;
import com.register.dao.QRUserAndDepotMapper;
import com.register.dao.UserMapper;
import com.register.pojo.QRDepot;
import com.register.pojo.QRUserAndDepot;
import com.register.pojo.SysUsers;
import com.register.service.QRUserAndDepotService;
import com.register.util.RSAUtils;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class QRUserAndDepotServiceImpl extends ServiceImpl<QRUserAndDepotMapper, QRUserAndDepot> implements QRUserAndDepotService {

    @Resource
    QRUserAndDepotMapper qrUserAndDepotMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    QRDepotMapper qrDepotMapper;
    @Value("${jwt.publicKey}")
    String publicKey;
    @Value("${jwt.privateKey}")
    String privateKey;

    public List<Map<String,Object>> getList(int type){
        List<Map<String,Object>> lists = new ArrayList<>();
        if (type == 0){
            lists = qrUserAndDepotMapper.getUserAndDepotOrderByDepId();
        }else {
            lists = qrUserAndDepotMapper.getUserAndDepotOrderByUserId();
        }

        return lists;
    }
    //上传csv文件 循环添加 场站与用户对应关系
    public String addUserAndDepot(List<String> lists){

        String msg ="";
        String phone = lists.get(4);
        String userName = lists.get(5);
        //判断用户与手机号 是否同时存在
        QueryWrapper queryWrapperUser = new QueryWrapper();
        queryWrapperUser.eq("username",userName);
        queryWrapperUser.eq("phone",phone);
        int userNum = userMapper.selectCount(queryWrapperUser);
        if (userNum == 0 ){
            //没有此用户 添加
            SysUsers sysUsers = new SysUsers();
            sysUsers.setUsername(userName);
            try{
                byte[] pwd = RSAUtils.encryptByPublicKey("1".getBytes(), Base64.decode(publicKey));
                sysUsers.setPassword(Base64.encode(pwd));
            }catch (Exception e){

            }
            sysUsers.setDescription("csv导入");
            sysUsers.setPhone(phone);
            sysUsers.setType("normal");
            userMapper.insert(sysUsers);
        }
        //添加场站  验证场站名称是否重复
        String depotName = lists.get(0);
        QueryWrapper queryWrapperDepot = new QueryWrapper();
        queryWrapperDepot.eq("name",depotName);
        int depotNum = qrDepotMapper.selectCount(queryWrapperDepot);
        if (depotNum == 0){
            //场站没有重复需要添加
            String abbreviation = lists.get(2).split("_")[0];
            String belong = lists.get(3);
            int type;
            if (depotName.contains("光")){
                type = 1;
            }else {
                type = 0;
            }
            QRDepot qrDepot = new QRDepot();
            qrDepot.setName(depotName);
            qrDepot.setType(type);
            qrDepot.setAbbreviation(abbreviation);
            qrDepot.setBelong(belong);
            qrDepotMapper.insert(qrDepot);
        }
        //检测是否存在此对应关系
        //验证用户
        SysUsers sysUsers = userMapper.selectOne(queryWrapperUser);
        //验证场站
        QRDepot qrDepot = qrDepotMapper.selectOne(queryWrapperDepot);
        //验证是否已经存在此对应关系
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userid",sysUsers.getId());
        queryWrapper.eq("depotid",qrDepot.getId());
        int num = qrUserAndDepotMapper.selectCount(queryWrapper);
        if (num == 0 ){
            QRUserAndDepot qrUserAndDepot = new QRUserAndDepot();
            qrUserAndDepot.setUserid(sysUsers.getId());
            qrUserAndDepot.setDepotid(qrDepot.getId());
            //添加对应关系
            qrUserAndDepotMapper.insert(qrUserAndDepot);
        }

        return msg;
    }
}
